package com.nj.redis.init;

import com.nj.redis.util.SpringbootContextUtil;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class initCache implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        ApplicationContext applicationContext = SpringbootContextUtil.getApplicationContext();
        //得到实现AbstractCache的所有类
        Map<String, AbstractCache> beanMap = applicationContext.getBeansOfType(AbstractCache.class);
        if (!beanMap.isEmpty()) {
            for (Map.Entry<String, AbstractCache> entry : beanMap.entrySet()) {
                //获得bean对象，调用initCache初始化缓存
                applicationContext.getBean(entry.getValue().getClass()).initCache();
            }
        }
    }
}
